//https://leetcode.cn/problems/sum-of-left-leaves/
package codeRandomThoughts.Test404左叶子之和;

import codeRandomThoughts.utils.TreeNode;

//二刷 后序遍历
public class Solution3 {
    //当前节点左叶子之和等于其左子树左叶子之和加右子树左叶子之和
    public int sumOfLeftLeaves(TreeNode root) {
        //终止条件
        if (root == null) {
            return 0;
        }

        //单层递归逻辑
        int leftSum = 0;
        int rightSum = 0;
        if (root.left != null && (root.left.left == null && root.left.right == null)) {
            leftSum = root.left.val;
        } else {
            leftSum = sumOfLeftLeaves(root.left);
        }
        rightSum = sumOfLeftLeaves(root.right);
        return leftSum + rightSum;
    }
}
